package com.hly.tool.cmd;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class Cmd
{

    /**
     * 执行 cmd 命令
     *
     * @param cmd
     * @return 是否执行成功
     */
    public static boolean execCmd(String cmd)
    {
        boolean flag = true;
        if (cmd == null || cmd.isEmpty())
        {
            return flag;
        }

        //使用BufferedReader是为了防止cmd命令阻塞。
        BufferedReader reader = null;
        try
        {
            Process process = Runtime.getRuntime().exec(cmd);
            reader = new BufferedReader(new InputStreamReader(process.getInputStream()));
            while ((reader.readLine()) != null)
            {
            }
            process.waitFor();
        } catch (Exception e)
        {
            flag = false;
            if (reader != null)
            {
                try
                {
                    reader.close();
                } catch (IOException ex)
                {
                    ex.printStackTrace();
                }
            }
            e.printStackTrace();
        }
        return flag;
    }


}
